import "mod_draw";
import "mod_dir";
import "mod_key";
import "mod_map";
import "mod_proc";
import "mod_grproc";
import "mod_screen";
import "mod_say";
import "mod_text";
import "mod_file";
import "mod_video";
import "mod_string";
import "mod_debug";

//---------------------------------------------------------------------------------------
GLOBAL
	
	// SETTINGS
	folder_png = "gfx/";
	folder_fpg = "fpg/";



	string png_name, fpg_name, filename, folder_name;
	int fpg, i, option;

END

//---------------------------------------------------------------------------------------
BEGIN

	set_mode(320, 240, 16, mode_window);
	
	write(0, 160, 0, 1,"FPG Tool");
	write(0, 10, 20, 3,"1 - Extraer PNGs de FPGs");
	write(0, 10, 40, 3,"2 - Crear FPGs a con PNGs");
	loop
	
		IF ( key(_esc) )
			exit();
		END
	
		// extraer PNGs de FPG
		IF ( key(_1) )
		
			LOOP
				// busco archivos FPG
				filename = glob ( folder_fpg + "*.fpg" );
				
				// si me quedo sin archivos salgo del bucle
				if ( filename == "" )
					break;
				end
				
				// sino, comienzo a exportar
				fpg = load_fpg( filename );
				
				say("----CARGADO " + filename + "...");
				
				// le saco la extension al filename
				folder_name = substr ( filename, 0, len(filename) - 4 );
				
				// creo una carpeta
				mkdir(folder_png);
				mkdir(folder_png + folder_name);
				
				// recorro el fpg y exporto todo como png
				FOR (i = 1; i <= 999; i++)
					if ( map_exists (fpg, i) )
					
						if ( i > 99 )
							png_name =  folder_png + folder_name + "/" + i + ".png";
						elseif (i > 9)
							png_name =  folder_png + folder_name + "/0" + i + ".png";
						else
							png_name =  folder_png + folder_name + "/00" + i + ".png";
						end
						
						png_save(fpg, i, png_name);
						
						say (png_name + " exportado...");
					end
				END
				
				// descargo el fpg
				unload_fpg ( filename );
				
				say("");
		
			END
		
		END
		
		// Crear FPGs a partir de PNGs
		IF ( key(_2) )
		
			LOOP
				
				// obtengo los nombres de las carpetas
				folder_name = glob ( folder_png + "*");
			
				// salgo del bucle al terminar
				if ( folder_name == "" )
					break;
				end
				
				// salteo el archivo si no es una carpeta
				if ( folder_name == "." or folder_name == ".." or fileinfo.directory == false )
					continue;
				end
				
				// creo el nuevo fpg vacio
				fpg = fpg_new();
				
				// agrego 1 a 1 los png
				FOR (i=1; i<= 999; i++)
				
					if ( i > 99 )
						if ( file_exists ( folder_png + folder_name + "/" + i + ".png" ) )
							png_name = load_png( folder_png + folder_name + "/" + i + ".png" );
							fpg_add(fpg, i, 0, png_name);
							say("agregado " + i + ".png a fpg " + fpg + "(" + folder_name + ")");
						end
					elseif( i > 9 )
						if ( file_exists ( folder_png + folder_name + "/0" + i + ".png" ) )
							png_name = load_png( folder_png + folder_name + "/0" + i + ".png" );
							fpg_add(fpg, i, 0, png_name);
							say("agregado 0" + i + ".png a fpg " + fpg + "(" + folder_name + ")");
						end
					else
						if ( file_exists ( folder_png + folder_name + "/00" + i + ".png" ) )
							png_name = load_png( folder_png + folder_name + "/00" + i + ".png" );
							fpg_add(fpg, i, 0, png_name);
							say("agregado 00" + i + ".png a fpg " + fpg + "(" + folder_name + ")");
						end
					end
				
				END
				
				// guardo el fpg
				fpg_save(fpg, folder_fpg + folder_name + ".fpg");
				say ( "guardado: " + folder_fpg + folder_name + ".fpg" );
				
			END
		
		END
		
		frame;
	end

END
